package com.example.springboot_blog.controller;

import com.example.springboot_blog.domain.Article;
import com.example.springboot_blog.domain.User;
import com.example.springboot_blog.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
//@RequestMapping("/manager")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/login")
    public String login() {
        return "user/login";
    }

    @GetMapping("/logout")
    public String logout() {
        return "user/logout";
    }

    @GetMapping("/register")
    public String register() {
        return "user/register";
    }

    @PostMapping("/register")
    public String registerUser(@RequestParam("email") String email, @RequestParam("username") String username, @RequestParam("password") String password, Model model) {
        if (userService.register(email, username, password)) {
            return "redirect:/login";

        } else {
            model.addAttribute("msg", "该用户已注册！");
            return "user/register";
        }
    }


    /**
     * @Paam:a
     * @Retrrn:a
     * 用户删除
     */
    @GetMapping("/manager/user/delete/{id}")
    public String deleteUser(@PathVariable("id") Integer id) {
        userService.deleteById(id);
        return "redirect:/manager/user";
    }

    /**
     * @Paam:a
     * @Retrrn:a
     * 用户管理页面
     */
    User user;
    @GetMapping("/manager/user")
    public String articleManager(Model model) {
        List<User> users = userService.selectUserList(user);
        model.addAttribute("users", users);
        return "/manager/user_manager";
    }
}
